package com.ajax.servlet;

import com.ajax.entity.User;
import com.ajax.service.UserService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckUserServlet extends HttpServlet {

    private UserService userService=new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CheckUerServlet ");
        String uname = req.getParameter("username");
        System.out.println("uname="+uname);
      //  resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("text/json;charset=utf-8");
        User user = userService.getUserByName(uname);
        if(user!=null){
            String strUser = new Gson().toJson(user);
            resp.getWriter().write(strUser);
        }else{
            resp.getWriter().write(new Gson().toJson(new User(-1, "", "", "")));
        }



//        if ("king".equals(uname)){
//            User king = new User(100, "king", "666", "king@sohu.com");
//            String strKing = new Gson().toJson(king);
//           resp.getWriter().write(strKing);
//        }else{
//            resp.getWriter().write("");
//        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doPost(req,resp);
    }
}
